from django.db import models
from annoying.fields import JSONField

PRIO_NORMAL = 1
PRIO_MEDIUM = 2
PRIO_HIGH = 3

EVERY_HOUR = 1
EVERY_DAY = 2
EVERY_WEEK = 3
EVERY_MONTH = 4
EVERY_YEAR = 5

ONCE = 1
EVERY = 2

class Link(models.Model):
    title = models.CharField(max_length=100)
    link = models.URLField(verify_exists=False, unique=True)

    # settings
    screenshot = models.BooleanField(default=False)
    dump = models.BooleanField(default=False)
    fullcheck = models.BooleanField(default=True)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return self.title + ': ' + self.link

class Provider(models.Model):
    name = models.CharField(max_length=20)
    ip_range = models.IPAddressField(max_length=120)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return 'Provider: %s; IP: %s' % (self.name, self.ip_range,)

class Scan(models.Model):
    title = models.CharField(max_length=40)
    provider = models.ForeignKey(to=Provider, null=True)
    start_date = models.DateTimeField(auto_now=True)
    end_date = models.DateTimeField(null=True)
    num_links = models.IntegerField(max_length=4)
    priority = models.SmallIntegerField(max_length=1, default=PRIO_NORMAL)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return '#%d Scan at: %s' % (self.pk, self.start_date)

class ProxyServer(models.Model):
    host = models.CharField(max_length=100)
    port = models.IntegerField(max_length=4)
    user = models.CharField(max_length=40, blank=True)
    password = models.CharField(max_length=40, blank=True)
    use = models.BooleanField()

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return self.host

    def __as_json__(self):
        return {
            'id': self.pk,
            'host': self.host,
            'port': self.port,
            'user': self.user,
            'password': self.password,
            'use': self.use
        }

class Schedule(models.Model):
    title = models.CharField(max_length=40)
    date_from = models.DateTimeField(null= True)
    date_to = models.DateTimeField(null=True)
    last_start = models.DateTimeField(null=True)
    frequency = models.IntegerField(default=ONCE)
    periods = models.IntegerField(default=EVERY_DAY)

    class Meta:
        app_label = 'core'

class ScheduleList(models.Model):
    scan = models.ForeignKey(to=Scan, null=True)
    schedule = models.ForeignKey(to=Schedule)
    link = models.ForeignKey(to=Link)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return 'List id: %d' % (self.pk,)

class ScheduleProxy(models.Model):
    schedule = models.ForeignKey(to=Schedule)
    proxy = models.ForeignKey(to=ProxyServer)

    class Meta:
        app_label = 'core'

class Result(models.Model):
    scan = models.ForeignKey(to=Scan)
    schedule = models.ForeignKey(to=Schedule, null=True)
    link = models.ForeignKey(to=Link)
    link_uri = models.URLField(max_length=100)
    aliaslist = models.CharField(max_length=255)
    ipaddrlist = models.CharField(max_length=255)
    header = JSONField() # { 'direct': HEADERS, 'proxy': HEADERS }
    screenshot = JSONField() # { 'direct': PATH, 'proxy': PATH }
    dump = JSONField() #  { 'direct': DUMP, 'proxy': DUMP }

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return 'Result for %s' % self.link_uri

class Variable(models.Model):
    key = models.CharField(max_length=20, verbose_name='Variable')
    value = models.CharField(max_length=1024)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return '%s: %s' % (self.key, self.value,)
